<template>
	<view class="content">
		<view class="main-wrap">
			<image src="../../static/index/16.png" mode="aspectFill" class="image1"></image>
			<view class="name">HUAWEI WATCH GT 2PRO</view>
			<view class="two">
				<image src="../../static/index/17.png" mode="aspectFill"></image>
				<view>已连接</view>
			</view>
			<view class="three" @click="connect()">
				<view>断开连接</view>
			</view>
		</view>
		<image src="../../static/index/15.png" mode="aspectFill" class="image-bottom"></image>
		<view v-for="(item,index) in inArray" :key="index" @click="createBLEConnection(item)" style="display: flex;justify-content: space-between;width: 100%;height: auto;">
			<view>
				name:{{item.name}}
			</view>
			<view>
				deviceId:{{item.deviceId}}
			</view>
		</view>	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				inArray: [],
				index:0
			}
		},
		methods: {
			// 蓝牙模块
			connect() {
				//初始化蓝牙
				uni.openBluetoothAdapter({
					success: (res) => {
						console.log(res, "初始化蓝牙");
						this.startBluetoothDevicesDiscovery();

					},
					fail: (res) => {
						var that = this;
						uni.showModal({
							title: '提示',
							content: '请开启本机蓝牙和定位'
						});
						console.log(res.errCode,res,'res.errCode');
						if (res.errCode === 10001) {
							//监听蓝牙适配器是否打开，若打开则自动搜索蓝牙设备
							uni.onBluetoothAdapterStateChange(function(res) {
								if (res.available) {
									that.startBluetoothDevicesDiscovery(); //开启蓝牙设备搜索
								}
							});
						}
					},
				});
			},
			//开启蓝牙设备搜索
			startBluetoothDevicesDiscovery() {
				var that = this;
				uni.startBluetoothDevicesDiscovery({
					allowDuplicatesKey: true, //允许重复上报同一个设备
					success: (res) => {
						console.log(res, "uni.startBluetoothDevicesDiscovery正在搜索设备");
						uni.showLoading({
							title: "正在搜索设备",
							duration: 2000
						});
						that.onBluetoothDeviceFound(); //监听新设备事件
				
					},
				});
			},

			//监听寻找到新设备的事件
			onBluetoothDeviceFound() {
				let that = this;
				uni.onBluetoothDeviceFound(() => {
					uni.getBluetoothDevices({
						success(res){
							res.devices.forEach(devicesa=>{
									
								// if(devicesa.deviceId == "08:C8:C2:0A:0F:F0"){
									console.log(devicesa.name,"------蓝牙设备名称",devicesa.deviceId,"------蓝牙设备ID");
								// }else{
								// 	console.log(devicesa.deviceId,"不等于");
								// }
							})
							
						}
					})
					// console.log(res,"getBluetoothDevices")
					// if (res) {
					// 	uni.hideLoading();
					// }
					// res.devices.forEach(device => {
					// 	//过滤掉没有名字的设备
					// 	if (!device.name && !device.localName) {
					// 		return
					// 	};
					// 	this.inArray.push(device);
					// 	console.log(this.inArray,"this.inArray");
						
					// })
				});
			},
			
			//这么操作是为了去除重复
			// const foundDevices = that.devices;
			// //将数据中的数组复制一份,利用动态绑定方式，不断复制最新的数组
			// console.log(foundDevices,'foundDevices666666');
			// console.log(device.deviceId,"device.deviceId");
			// const idx = that.inArray(foundDevices, 'deviceId', device.deviceId);
			// console.log(idx,"idx666");
			// if (idx === -1) {
			// 	//TSH-01(BLE) 蓝牙设备型号 根据需求选择设备型号
			// 	if (device.name == "TSH-01(BLE)") {
			// 		console.log(device,"蓝牙设备的型号");
			// 		that.handleData02(device.advertisData); //蓝牙接收的数据
			// 		that.devices.push(device); //数组里没有的的就向里面添加数据，保证没有重复   
			// 	}
			// 	// that.devices.push(device);
			// }
			
			
			
			
			createBLEConnection(item) {
				let that = this;
				console.log(666);
				// that.$refs.loading.open();
				// this.chooseDevice = item; // item 包含 item.name(蓝牙名称) 和 item.deviceId(蓝牙MAC)
				console.log(item, item.deviceId, "item666666666666666666666");
				uni.createBLEConnection({
					deviceId: item.deviceId,
					success: (res) => {
						setTimeout(() => {
							console.log("连接成功----", res);
							uni.showModal({
								title: '提示',
								content: '连接成功'
							})
							uni.stopBluetoothDevicesDiscovery(); //停止搜索蓝牙设备
							that.getBLEDeviceServices(item.deviceId); //获取服务
							that.$refs.loading.close();
						}, 1000);
					},
					fail: (err) => {
						that.$refs.loading.close();
						console.log("连接失败----", err);
						return
					}
				})
			},

			getBLEDeviceServices(deviceId) {
				uni.getBLEDeviceServices({
					deviceId: deviceId, //连接设备的devideid值
					success: (res) => {
						console.log(res, "获取服务")
						//serviceId固定，获取蓝牙设备某个服务中所有特征值【读写属性的】
						this.getBLEDeviceCharacteristics(deviceId, this.serviceId)
					},
					fail: (err) => {
						uni.showToast({
							title: '获取服务失败',
							icon: 'error',
							duration: 2000
						})
						console.log("获取服务失败----", err);
						return
					}
				})
			},
		}
	}
</script>

<style>
</style>
<style>
	page {
		width: 100%;
		height: 100%;
		margin: 0;
		padding: 0;
		background-color: #f7f7f7;
	}
</style>
<style lang="scss" scoped>
	.content {
		height: 100vh;
		overflow: auto;
	}

	.main-wrap {
		width: 650rpx;
		height: auto;
		margin: 295rpx auto 20rpx auto;
		display: flex;
		align-items: center;
		flex-direction: column;

		.image1 {
			width: 100%;
			height: 259rpx;
		}

		.name {
			margin-top: 86rpx;
			font-size: 26rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
		}

		.two {
			width: 106rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 23rpx;
			margin-top: 30rpx;

			image {
				width: 20rpx;
				height: 23rpx;
			}

			view {
				font-size: 22rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #32529B;
			}
		}

		.three {
			width: 163rpx;
			height: 163rpx;
			margin-top: 59rpx;
			background: #FFFFFF;
			box-shadow: 0 5rpx 51rpx 0 rgba(98, 98, 98, 0.11);
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;

			view {
				text-align: justify;
				width: 50rpx;
				height: 60rpx;
				font-size: 25rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}
		}
	}

	.list {
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		width: 50%;
	}

	.image-bottom {
		width: 750rpx;
		height: 315rpx;
		position: absolute;
		bottom: 0;
	}
</style>